package com.zhf.servlet.student;

import com.zhf.entity.Student;
import com.zhf.service.StudentService;
import com.zhf.service.impl.StudentServiceImpl;
import com.zhf.utils.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "StudentUpdateServlet", urlPatterns = "/studentUpdate")
public class StudentUpdateServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 数据接收
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        String birthDay = request.getParameter("birthDay");
        String email = request.getParameter("email");
        String remark = request.getParameter("remark");
        String currentPage = request.getParameter("currentPage");
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setGender(gender);
        student.setBirthDay(DateUtil.getDateFromString(birthDay));
        student.setEmail(email);
        student.setRemark(remark);
        int i = studentService.updateStudent(student);
        if(i > 0){
            request.setAttribute("msg", "修改成功");
            request.setAttribute("code", "100");
        }
        else{
            request.setAttribute("msg", "修改失败，未找到该员工编号！");
            request.setAttribute("code", "101");
        }
        request.setAttribute("currentPage", currentPage);
        request.getRequestDispatcher("index.do").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
